package com.ccloud.auth.modules.controller;

import com.ccloud.api.system.service.ISysLogService;
import com.ccloud.api.system.service.ISysUserService;
import com.ccloud.common.core.reqres.response.Result;
import com.ccloud.common.core.utils.CommonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * 登录
 *
 * @author chentailai
 */
@RestController
@Api(tags = "用户登录")
@Slf4j
public class LoginController {
    @Resource
    private ISysUserService sysUserService;

    @Resource
    private ISysLogService logService;

    /**
     * 获取访问量
     *
     * @return Result<Map < String, Object>>
     */
    @GetMapping("/loginfo")
    public Result<Map<String, Object>> logInfo() {
        Result<Map<String, Object>> result = new Result<>();
        Map<String, Object> obj = new HashMap<>(4);
        // 获取一天的开始和结束时间
        Calendar calendar = new GregorianCalendar();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date dayStart = calendar.getTime();
        calendar.add(Calendar.DATE, 1);
        Date dayEnd = calendar.getTime();
        // 获取系统访问记录
        Long totalVisitCount = logService.findTotalVisitCount();
        obj.put("totalVisitCount", totalVisitCount);
        Long todayVisitCount = logService.findTodayVisitCount(dayStart, dayEnd);
        obj.put("todayVisitCount", todayVisitCount);
        Long todayIp = logService.findTodayIp(dayStart, dayEnd);
        obj.put("todayIp", todayIp);
        result.setResult(obj);
        result.success("登录成功");
        return result;
    }

    /**
     * 获取访问量
     *
     * @return Result<List < Map < String, Object>>>
     */
    @GetMapping("/visitInfo")
    public Result<List<Map<String, Object>>> visitInfo() {
        Result<List<Map<String, Object>>> result = new Result<>();
        Calendar calendar = new GregorianCalendar();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        Date dayEnd = calendar.getTime();
        calendar.add(Calendar.DAY_OF_MONTH, -7);
        Date dayStart = calendar.getTime();
        List<Map<String, Object>> list = logService.findVisitCount(dayStart, dayEnd);
        result.setResult(CommonUtil.toLowerCasePageList(list));
        return result;
    }


    /**
     * 短信登录接口
     *
     * @param jsonObject Map<String, String>
     * @return Result<String>
     */
    @PostMapping(value = "/sms")
    public Result<Map<String, Object>> sms(@RequestBody Map<String, String> jsonObject) {
        Result<Map<String, Object>> result = sysUserService.smsLogin(jsonObject);
        return result;
    }

    /**
     * 手机号登录接口
     *
     * @param jsonObject ap<String, String>
     * @return Result<Map < String, Object>>
     */
    @ApiOperation("手机号登录接口")
    @PostMapping("/phoneLogin")
    public Result<Map<String, Object>> phoneLogin(@RequestBody Map<String, String> jsonObject) {
        Result<Map<String, Object>> result = sysUserService.phoneLogin(jsonObject);
        return result;
    }

}